<%@page import="java.sql.ResultSet"%>
<%@page import="com.User.Dao.User"%>
<%@page import="com.Servlet.Dao.usersEntity"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The library</title>
	<link rel="stylesheet" type="text/css" href="css/Main.css">
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
	<script src="js/Main.js" type="text/javascript"></script>
</head>
<body>
<%
	String user = (String)request.getAttribute("userSno"); 
	String showInfo = (String)request.getAttribute("flag");
	ResultSet ms = null;
	if(showInfo != null && showInfo.equals("showRecord")){
		ms = (ResultSet)request.getAttribute("showFlag");
	}
	User userInfo = usersEntity.enqury(user);
	ResultSet rs = null;
	rs = (ResultSet)request.getAttribute("ResultSet");
	if(userInfo == null){
		userInfo = new User();
	}
%>
<div class='container'>
	<div class="welcome">
		<h1>Welcome</h1>
	</div>
	<div class="extendContain">
		<div class="functionToUserInfo">
			<p></p>
		</div>
		<div class="functionToBorrowing">
			<p></p>
		</div>
		<div class="functionToHotBook">
			<p></p>
		</div>
		<div class="functionToRight">
			<p></p>
		</div>
	</div>
	<div id="time"></div>
  <div class='container_ui'>
  	<div class="extendInfo">
		<ul>
			<li><p></p><%=userInfo.getUserName() %></li>
			<li><p></p><%=userInfo.getIdentity() %></li>
			<li><p></p>$<%=userInfo.getMoney() %></li>
			<li><p></p><a href="getBookServlet?sno=<%=userInfo.getUser()%>&user=<%=user%>">已借阅图书</a></li>
			<li id="remind"><p></p><a href="remindServlet?sno=<%=userInfo.getUser()%>&user=<%=user%>">还书提醒(7天)</a></li>
		</ul>
	</div>
	<div class="extendBook">
		<ul>
			<li id="borrowBook"><p></p><a href="borrowBookServlet?user=<%=user%>">借阅图书</a></li>
			<li id="upDate"><p></p><a href="upDateServlet?sno=<%=userInfo.getUser()%>&user=<%=user%>">到期图书</a></li>
		</ul>
	</div>
	<div class="extendHot">
		<ul>
			<li id="heat"><p></p><a href="heatServlet?user=<%=user%>">热度图书排名</a></li>
		</ul>
		<ul>
			<li><p></p><a href="bookRankServlet?user=<%=user%>">书籍排名</a></li>
		</ul>
	</div>
	<div class="extendRight">
		<ul>
			<li><p></p><a href="bookUpdateServlet?user=<%=user%>">插入书籍信息</a></li>
		</ul>
		<ul>
			<li><p></p><a href="bookDropServlet?user=<%=user%>">删除书籍信息</a></li>
		</ul>
		<ul>
			<li><p></p><a href="changeMonneyServlet?user=<%=user%>">修改用户余额</a></li>
		</ul>
		<ul>
			<li><p></p><a href="showRecordInfoServlet?user=<%=user%>">显示借阅记录</a></li>
		</ul>
	</div>
	<div class='container_ui__heading'>
		<div class="top">
			<!-- 用户logo -->
			<div class="topUser">
				<div class="topLeft">
					<img src="images/2.jpg" width="100%" height="100%">
				</div>
				<div class="topRight">
					<p></p>
				</div>
			</div>
			<!-- 搜索栏 -->
			<div class="topSearch">
				<form action="" id="ToSearch" method="post">
					<input type="text" name="userSno" value=<%=user %> style="display: none;">
					<input type="text" name="serchInfo" placeholder="Author" class="sAuthor">
					<input type="text" name="serchInfo" placeholder="Style" class="sStyle">
					<input type="text" name="serchInfo" placeholder="Books" class="sBooks">
					<input type="submit" value="" style="font-family: 'ashen'; font-size: 17px;">
				</form>
			</div>
		</div>
		<!-- 插入书籍名称 -->
		<form action="bookInsertServlet" id="insertInfo" method="post">
			<input type="text" placeholder="书编号" name="bookInfo">
			<input type="text" placeholder="书名" name="bookInfo">
			<input type="text" placeholder="作者" name="bookInfo">
			<input type="text" placeholder="类型" name="bookInfo">
			<input type="text" placeholder="出版时间" name="bookInfo">
			<input type="text" placeholder="出版社信息" name="bookInfo">
			<input type="text" value="可借" style="display: none" name="bookInfo">
			<input type="text" placeholder="URL(Default: images/2.jpg)" name="bookInfo">
			<input type="submit" id="iSubmit" value="" style="font-family: 'ashen'; font-size: 17px;">
			<input type="text" name="userSno" value=<%=user%> style="display: none;">
		</form>
		<!-- 插入图书信息 -->
		<form action="bookStyleInsertServlet" id="insertInfoStyle" method="post">
			<%
				String book_style = (String)request.getAttribute("book_style");
				if(book_style != null){
			%>
			<input type="text" placeholder="图书类型" name="bookInfo" value=<%=book_style %> readonly="readonly">
			<%
				}
			%>
			<input type="text" value="0" style="display: none" name="bookInfo">
			<input type="text" placeholder="图书年龄段" name="bookInfo">
			<input type="text" placeholder="图书信息" name="bookInfo">
			<input type="text" placeholder="图书" name="bookInfo">
			<input type="text" placeholder="URL(Default: images/1.jpg)" name="bookInfo">
			<input type="submit" id="iS_Submit" value="" style="font-family: 'ashen'; font-size: 17px;">
			<input type="text" name="userSno" value=<%=user%> style="display: none;">
		</form>
		<!-- 删除图书 -->
		<form action="dropInfoStyleServlet" id="dropInfoStyle" method="post">
			<input type="text" placeholder="书籍编号" name="book_sno">
			<input type="text" name="userSno" value=<%=user%> style="display: none;">
			<input type="submit" id="drop_Submit" value="" style="font-family: 'ashen'; font-size: 17px;">
		</form>
		<!-- 修改余额 -->
		<form action="cMonneyToUserServlet" id="changeMonney">
			<input type="text" placeholder="用户账号" name="userSno">
			<input type="text" placeholder="修改余额" name="money">
			<input type="submit" id="change_Submit" value="" style="font-family: 'ashen'; font-size: 17px;">
		</form>
		<div id="table" style="display: none;">
		<%
			if(ms != null){
				%>
				 <table  width="350" border="1" cellpadding="0" >
			  		<tr>
			  			<th>图书编号</th>
				  		<th>借书者编号</th>
				  		<th>开始借阅时间</th>
				  		<th>归还时间</th>
				  		<th>是否超时  </th>
			  		</tr>
			     <%
			     	while(ms.next()){
			     %>
			       <tr>
				       <td><%=ms.getString("book_no") %></td>
				       <td><%=ms.getString("reader_no") %></td>
				       <td><%=ms.getDate("book_begintime") %></td>
				       <td><%=ms.getDate("book_returntime") %></td>
				       <td><%=ms.getString("reader_fine") %></td>
				   </tr>
				  <%
			     	}
				  %>
			    </table>
		<% 
		request.setAttribute("showFlag", null);
			}
		%>
		</div>
		<!-- 书名信息展示 -->
		<form action="" id="formToFunction" method="post">
			<div id="bigBoxToShow">
				<input type="text" name="userSno" value=<%=user%> style="display: none;">
				<%
					if(rs != null){
						while(rs.next()){
				%>	
				<input id=<%=rs.getString("book_no") %> type='checkbox' name="checkBook" value=<%=rs.getString("book_no") %>>
				<label for=<%=rs.getString("book_no")%> href='#move' class="forBox">
				  <div class='container_ui__item'>
				  	<div class='face'>
					  <img src=<%=rs.getString("book_cover")%>>
					  <div class='color_bar one'>
						<p style="font-family: 'ashen'; font-size: 36px; color: green; line-height: 20px;"></p>
						<span>Read</span>
					  </div>
					</div>
					<h2>
					  <%=rs.getString("book_name") %>
					</h2>
					<div class='dot active'></div>
					<h3><%=rs.getString("book_writer") %></h3>
					<h4><%=rs.getString("book_style") %></h4>
				  </div>
				  <div class='container_ui__expand' id='close'>
					<div class='heading'>
					  <div class='heading_head'></div>
					  <label for='message-1'>
						x
					  </label>
					</div>
					<div class='body'>
					  <div class='user'>
						<div class='face'>
						  <img src=<%=rs.getString("book_cover")%>>
						</div>
						<div class='details'>
						  <h2><%=rs.getString("book_writer")%></h2>
						  <h3><%=rs.getString("book_style")%></h3>
						</div>
					  </div>
					  <div class='content'>
						<p><b>Info text</b></br><%=rs.getString("book_from")%></p>
						<span>Reply:</span>
						<textarea></textarea>
					  </div>
					</div>
				  </div>
				</label>
				<%
					String Flag = (String)request.getAttribute("flag");
					if(rs.getString("book_status") != null && rs.getString("book_status").equals("已借") && Flag != null && Flag.equals("borrowBook")){
						ResultSet os = usersEntity.fromBookToReader(rs.getString("book_no"));
						if(os.next()){	
						%>
							<script>
								var value = "<%=rs.getString("book_no") %>";
								var bnt = document.getElementById(value);
								bnt.onclick = function(){
									alert("<%=os.getString("reader_no")%>" + "用户已借阅");
									return false;
								}
							</script>
						<%
						}
					}
						}
					}
				%>
			</div>
		<input type="submit" id="checkSubmit" value="提交" style="font-family: 'ashen'; font-size: 26px;">
		<%
		String flag = (String)request.getAttribute("flag");
		String checkFlag = (String)request.getAttribute("checkFlag");
		String check = (String)request.getAttribute("check");
		String dropInfoFlag = (String)request.getAttribute("dropInfoFlag");
		if(flag != null && flag.equals("getBook")){
			%>
				<script>
					document.getElementById("ToSearch").action = "searchByGetBook";
					document.getElementById("formToFunction").action = "readerReturnBook";
				</script>
			<% 
		}else if(flag != null && flag.equals("borrowBook")){
			%>
			<script>
				document.getElementById("ToSearch").action = "searchByBorrowBook";
				document.getElementById("formToFunction").action = "readerBorrowBook";
			</script>
		<% 
		}else if(flag != null && flag.equals("remindBook")){
			%>
			<script>
				document.getElementById("formToFunction").action = "readerToRenewal";
			</script>
		<% 
		}
		if(flag != null && flag.equals("getBook")){	
			%>
				<script src="js/Main.js" type="text/javascript"></script>
				<script>
					showBrrow();
					var shower = document.getElementsByClassName("topSearch");
					shower[0].style.display = "none";
					var submit = document.getElementById("checkSubmit");
					submit.style.display = "block";
					var hide = document.getElementsByClassName("welcome");
					hide[0].style.display = "none";
					var bnt1 = document.getElementsByClassName("extendInfo");
					style = bnt1[0].style.display;
					moveToFunction(style, bnt1[0], 0);
				</script>
			<% 
		}else if(flag != null && flag.equals("borrowBook")){
			%>
				<script src="js/Main.js" type="text/javascript"></script>
				<script>
					showBrrow();
					var submit = document.getElementById("checkSubmit");
					submit.style.display = "block";
					var hide = document.getElementsByClassName("welcome");
					hide[0].style.display = "none";
					var bnt1 = document.getElementsByClassName("extendBook");
					style = bnt1[0].style.display;
					moveToFunction(style, bnt1[0], 0);
				</script>
			<% 
		}else if(flag != null && flag.equals("SearchByGetBook")){ 
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				showBrrow();
				var submit = document.getElementById("checkSubmit");	
				submit.style.display = "none";
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendBook");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
				document.getElementById("formToFunction").action = "readerReturnBook";
			</script>
			<% 
		}else if(flag != null && flag.equals("SearchByBorrowBook")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				showBrrow();
				var submit = document.getElementById("checkSubmit");
				submit.style.display = "block";
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendBook");
				document.getElementById("formToFunction").action = "readerBorrowBook";
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("updateInfo")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				alert("Operation is successful");
				closeBrrow();
				var submit = document.getElementById("checkSubmit");
				submit.style.display = "none";
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "block";
				var bnt1 = document.getElementsByClassName("extendBook");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("returnInfo")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				alert("Operation is successful");
				closeBrrow();
				var submit = document.getElementById("checkSubmit");
				submit.style.display = "none";
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "block";
				var bnt1 = document.getElementsByClassName("extendBook");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("remindBook")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendInfo");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
				var submit = document.getElementById("checkSubmit");
				submit.style.display = "block";
			</script>
			<% 
		}else if(flag != null && flag.equals("upDateBook")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendInfo");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("heatBook")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendHot");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("rankBook")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt1 = document.getElementsByClassName("extendHot");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("monney")){
			String fa = (String)request.getAttribute("cMonney");
			if(fa.equals("true")){
				%>
				<script>
					alert("Operation is successful");
				</script>
				<% 
			}else{
				%>
				<script>
					alert("Users don't exist");
				</script>
				<% 
			}
			%>
			<% 
		}else if(flag != null && flag.equals("dropInfoStyle") && dropInfoFlag.equals("True")){
			%>
			<script>
				alert("Operation is successful");
			</script>
			<% 
		}else if(flag != null && flag.equals("dropInfoStyle") && dropInfoFlag.equals("False")){
			%>
			<script>
				alert("Operation is failure");
			</script>
			<% 
		}else if(flag != null && flag.equals("bookInsert") && check.equals("true")){
			%>
			<script>
				alert("Operation is successful");
			</script>
			<% 
		}else if(flag != null && flag.equals("bookInsert") && check.equals("false")){
			%>
			<script>
				alert("The operation failure");
			</script>
			<% 
		}else if(flag != null && flag.equals("bookStyleInsert")){
			%>
			<script>
				alert("Success");
			</script>
			<% 
		}else if(flag != null && flag.equals("bookUpdate") && userInfo.getIdentity().equals("管理员")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt = document.getElementById("insertInfo");
				bnt.style.display = "block";
				var bnt1 = document.getElementsByClassName("extendRight");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && (flag.equals("bookUpdate") || (flag.equals("bookDrop") && !userInfo.getIdentity().equals("管理员")) || (flag.equals("changeMoney") && !userInfo.getIdentity().equals("管理员")) || (flag.equals("showRecord") && !userInfo.getIdentity().equals("管理员")))){
			%>
			<script>
				alert("Insufficient permissions");
			</script>
			<% 
		}else if(flag != null && flag.equals("bookDrop") && userInfo.getIdentity().equals("管理员")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt = document.getElementById("dropInfoStyle");
				bnt.style.display = "block";
				var bnt1 = document.getElementsByClassName("extendRight");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
		}else if(flag != null && flag.equals("changeMoney") && userInfo.getIdentity().equals("管理员")){
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				showNormal();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt = document.getElementById("changeMonney");
				bnt.style.display = "block";
				var bnt1 = document.getElementsByClassName("extendRight");
				style = bnt1[0].style.display;
				moveToFunction(style, bnt1[0], 0);
			</script>
			<% 
			/*todo:*/
		}else if(showInfo != null && showInfo.equals("showRecord") && userInfo.getIdentity().equals("管理员")){
			%>
			<script>
				closeBrrow();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt = document.getElementById("insertInfo");
				bnt.style.display = "none";
				var mn = document.getElementById("table");
				if(mn != null){
					mn.style.display = "block";
				}
			</script>
			<% 
		}
		/*
			todo: yes
		*/
		if(flag != null && flag.equals("bookInsert") && checkFlag.equals("T") && check.equals("true")){
			request.setAttribute("checkFlag", "F");
			%>
			<script src="js/Main.js" type="text/javascript"></script>
			<script>
				closeBrrow();
				var hide = document.getElementsByClassName("welcome");
				hide[0].style.display = "none";
				var bnt = document.getElementById("insertInfoStyle");
				bnt.style.display = "block";
				var bnt = document.getElementById("insertInfo");
				bnt.style.display = "none";
			</script>
			<% 
		}
		if(flag != null && flag.equals("updateInfo")){
			String cp = (String)request.getAttribute("statusFlag");
			if(cp.equals("false")){
				%>
				<script>
					alert("可借书籍已满");
				</script>
				<% 	
			}
		}
		/*
			todo:
		*/
		if(flag != null && flag.equals("getBook")){
			%>
			<script>
				var bnt = document.getElementsByClassName("serchToInfo")[0];
				bnt.style.display = "block";
				var bnts = document.getElementsByClassName("serchInfo");
				alert(bnts.length);
				for(var i = 0; i < bnts.length; i++){
					bnts[i].style.display = "none";
				}
				alert();
			</script>
			<% 
		}else if(flag != null && flag.equals("borrowBook")) {
			%>
			<script>
				var bnt = document.getElementsByClassName("serchToInfo")[0];
				bnt.style.display = "none";
				var bnts = document.getElementsByClassName("serchInfo");
				for(var i = 0; i < bnts.length; i++){
					bnts[i].style.display = "blcok";
				}
			</script>
			<%
		}
		String kFlag = (String)request.getAttribute("renewalFlag");
		if(kFlag != null && kFlag.equals("true")){
			%>
				<script>
				alert("Success");
				</script>
			<% 
		}
		
		%>
		</form>
	</div>
</div>
</div>
</body>
</html>